var Tween = require('geoTweenMoveTo');
var SequenceRunner = require('geoSequenceRunner');

var TweenFromTo = cc.Class({
    extends: cc.Component,

    properties: {
        target: {
            default: null,
            type: cc.Node,
            tooltip: '渐变对象，默认为自身节点。'
        },
        from: {
            default: null,
            type: cc.Node,
            tooltip: '以此节点为起始态'
        },
        to: {
            default: null,
            type: cc.Node,
            tooltip: '以此节点为终止态'
        },
        duration: {
            default: 1,
            type: cc.Float,
            tooltip: '渐变时长'
        },
        useMove: {
            default: true,
            tooltip: '是否渐变位置'
        },
        useScale: {
            default: false,
            tooltip: '是否渐变缩放'
        },
        useRotation: {
            default: false,
            tooltip: '是否渐变旋转'
        },
        repeat: {
            default: 1,
            type: cc.Integer,
            tooltip: '循环次数(小于1则无限循环)。'
        },
        completeEvents: {
            default: [],
            type: [cc.Component.EventHandler],
            tooltip: '渐变完成时的调用(无限循环不触发此事件)。'
        },
        startOnLoad: {
            default: true,
            tooltip: '是否自动开始'
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        if (this.startOnLoad == true)
            this.doTween();
    },

    doTween(complete) {
        var target = this.target || this.node;
        if (this._tween == null)
            this._tween = target.addComponent(Tween);
        var tw = this._tween;
        var repeat = this.repeat;
        if (repeat > 0) {
            var sqRunner = new SequenceRunner();
            var funcs = [];
            for (var n = 0; n < repeat; ++n) {
                // 如果是其它模式，则可以在此操作
                var from = this.from;
                var to = this.to;
                funcs.push(
                    this._doTweenOnce.bind(this,
                        tw, from, to, this.duration,
                        this.useMove, this.useScale, this.useRotation,
                        sqRunner.next.bind(sqRunner))
                );
            }
            //
            funcs.push(
                (()=>{
                    // 调用传入的complete
                    complete && complete();
                    // 调用关联的complete
                    this._onTweenComplete();
                }).bind(this));
            //
            sqRunner.run(funcs);
        } else {
            // TODO 目前的处理方式不适合做播放模式处理
            // 无限播放
            this._doTweenOnce(tw, this.from, this.to, this.duration,
                this.useMove, this.useScale, this.useRotation,
                this.doTween.bind(this));
        }
    },

    _doTweenOnce(tween, from, to, duration, useMove, useScale, useRotation, complete) {
        //
        tween.setToNode(from, useMove, useScale, useRotation);
        //
        tween.doTweenToNode(to, duration, complete, useMove, useScale, useRotation);
    },

    _onTweenComplete() {
        var e = new cc.Event.EventCustom('geo-tween-from-to-complete');
        //
        cc.Component.EventHandler.emitEvents(this.completeEvents, e);
    },
});
